package ai.tubes.iris.helper;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class Utilities {
	public static void initializeApps(Context context){
		BASE_DIRECTORY = context.getFilesDir().toString();
	}
	
	public static Bitmap downloadImage(String urlStr){
		boolean gagal = true;
		int attempt = 0;

		while (gagal && attempt < Utilities.DOWNLOAD_ATTEMPT){
			try {
				URL url = new URL(urlStr);
				HttpURLConnection connection = (HttpURLConnection)url.openConnection();
				connection.setDoInput(true);
				connection.connect();
		
				InputStream input = connection.getInputStream();
				gagal = false;

				Log.i("Chapter", "Done downloadding image");
				return BitmapFactory.decodeStream(input);
				
			} catch (Exception ex){
				++attempt;
			}
		}
		return null;
	}
	
	public static byte[] sTempImageByteArray;
	public static final int DOWNLOAD_ATTEMPT	= 5;
	public static final int SPLASH_TIME			= 1000;
	public static final String PREFERENCES 		= "PREFERENCES";
	public static String BASE_DIRECTORY;
}
